package com.cmwa.scbp.sys.msg.filter;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cmwa.scbp.sys.msg.vo.ManualOperationParamsVo;
import com.cmwa.scbp.sys.msg.vo.MsgWaitSendVo;

public class MsgFilterChainImpl implements MsgFilterChain {

	private final List<? extends MsgFilterConfig> filters;
	private int currentPosition = 0;
	private MsgFilterResult msgFilterResult;

	public MsgFilterChainImpl(List<? extends MsgFilterConfig> filters) {
		this.filters = filters;
	}
	
	@Override
	public void setMsgFilterResult(MsgFilterResult filterResult) {
		this.msgFilterResult = filterResult;
	}

	public MsgFilterResult getMsgFilterResult() {
		return msgFilterResult;
	}

	public void doFilter(List<MsgWaitSendVo> sendVos, ManualOperationParamsVo params) {
		// 没有过滤器
		if (CollectionUtils.isEmpty(filters)) {
			return;
		}
		
		// 过滤链走完
		if (currentPosition == filters.size()) {
			return;
		}
		
		currentPosition++;
		MsgFilterConfig filterConfig = filters.get(currentPosition - 1);
		MsgFilter nextFilter = filterConfig.getMsgFilter();
		nextFilter.doFilter(sendVos, params, filterConfig.getFilterParam(), this);
	}

}
